local guanwei = fk.CreateSkill {
  name = "rfenghou_3d15j__guanwei",
  tags = { Skill.Compulsory },
  extra = {
    ---@class rfenghou_3d15j__setGuanweiMarkParams
    ---@field target ServerPlayer @ 标记所有人
    ---@field card_table [integer,integer][] @ 最终的牌组
    ---@field skill_name string @ 操作的技能名
    ---@field skip? boolean @ 是否跳过将移除牌置于弃牌堆的流程

    ---为观微量身定做的标记赋值函数
    ---@param player ServerPlayer
    ---@param params rfenghou_3d15j__setGuanweiMarkParams
    ---@return integer[] @ 需要被移除的牌
    rfenghou_3d15j__setGuanweiMark = function(player, params)
      local target, card_table, skip = params.target, params.card_table, params.skip
      local skill_name = params.skill_name
      local room = player.room
      local mark_table = target:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo")
      if mark_table == 0 then
        mark_table = {
          list_names = {},
          list_cards = {},
          column = 2,
          buff = {}
        }
      end
      mark_table = table.clone(mark_table)
      -- 初始化
      mark_table.buff = {}

      local list_names = mark_table.list_names
      local list_cards = mark_table.list_cards
      local buff = mark_table.buff
      local to_add = table.connect(table.unpack(card_table))
      local to_remove = table.connect(table.unpack(list_cards))
      local same = {}
      -- 处理差别
      for _, cid in ipairs(to_remove) do
        if table.removeOne(to_add, cid) then
          table.insert(same, cid)
        end
      end
      for _, cid in ipairs(same) do
        table.removeOne(to_remove, cid)
      end

      for index = 1, math.max(#list_names, #card_table), 1 do
        local cards = card_table[index]
        local orig_cards = list_cards[index] or {}
        if cards == nil then
          list_names[index] = nil
          list_cards[index] = nil
        else
          -- 比对buff
          local this_buffs = {}
          local card1 = Fk:getCardById(cards[1])
          local card2 = Fk:getCardById(cards[2])
          if card1:compareSuitWith(card2) then
            table.insertIfNeed(this_buffs, Fk:translate("#rfenghou_3d15j__panjun_haomo_buff_draw"))
            table.insertIfNeed(buff, 1)
          end
          if card1.type == card2.type then
            table.insertIfNeed(this_buffs, Fk:translate("#rfenghou_3d15j__panjun_haomo_buff_time"))
            table.insertIfNeed(buff, 2)
          end
          if card1.number ~= 0 and card2.number ~= 0 then
            if card1.number % 2 == card2.number % 2 then
            table.insertIfNeed(this_buffs, Fk:translate("#rfenghou_3d15j__panjun_haomo_buff_maxs"))
              table.insertIfNeed(buff, 3)
            end
          end

          local txt
          if #this_buffs ~= 0 then
            txt = table.concat(this_buffs, "/")
          else
            txt = "#rfenghou_3d15j__panjun_haomo_buff_none"
          end
          list_names[index] = "#rfenghou_3d15j__panjun_haomo_haomo:::" .. txt
          list_cards[index] = table.simpleClone(cards)
        end
      end

      table.sort(buff)
      if not skip and next(to_remove) then
        room:moveCardTo(to_remove, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skill_name, nil, true, player)
      end
      if next(to_add) then
        target:addToPile("#rfenghou_3d15j__panjun_haomo", to_add, true, skill_name, player)
      end
      -- 我才是神酱！
      room:setPlayerMark(target, "@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo", mark_table)
      return to_remove
    end
  }
}

Fk:addQmlMark{
  name = "rfenghou__haomo_cardlist",
  how_to_show = function(name, value, player)
    if type(value) ~= "table" then return " " end
    local txt
    if #(value.buff or {}) ~= 0 then
      local tbl = {"#rfenghou_3d15j__panjun_haomo_buff_draw", "#rfenghou_3d15j__panjun_haomo_buff_time", "#rfenghou_3d15j__panjun_haomo_buff_maxs"}
      txt = table.concat(table.map(value.buff, function(i) return Fk:translate(tbl[i]) end), "|")
    else
      txt = "#rfenghou_3d15j__panjun_haomo_buff_none"
    end
    return txt
  end,
  qml_path = "packages/aaa_Romantic/qml/CardListBox",
}

guanwei:addLoseEffect(function(self, player, is_death)
  if #player:getPile("#rfenghou_3d15j__panjun_haomo") ~= 0 then
    player:throwAllCards("s", "#rfenghou_3d15j__panjun_haomo")
  end
  player.room:setPlayerMark(player, "@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo", 0)
end)

guanwei:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(guanwei.name, true)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:drawCards(6, guanwei.name)
    local card_table = {}
    local cards = player:getCardIds("he")
    for i = 1, 3, 1 do
      local to_put_cards = room:askToCards(player, {
        min_num = 2,
        max_num = 2,
        pattern = tostring(Exppattern:new{ id = cards }),
        cancelable = false,
        skill_name = guanwei.name,
        prompt = "#rfenghou_3d15j__guanwei-invoke:::" .. i
      })
      if #to_put_cards == 2 then
        for _, cid in ipairs(to_put_cards) do
          room:setCardMark(Fk:getCardById(cid), "@rfenghou_3d15j__panjun_haomo-inhand", i)
          table.removeOne(cards, cid)
        end
        table.insert(card_table, to_put_cards)
      end
    end
    guanwei.extra.rfenghou_3d15j__setGuanweiMark(player, {
      target = player,
      card_table = card_table,
      skill_name = guanwei.name
    })
  end
})
guanwei:addEffect(fk.DrawNCards, {
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and not data.phase_end then
      return table.contains(player:getTableMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo").buff or {}, 1)
    end
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n + 1
  end
})
guanwei:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope, card, to)
    if player and card and card.trueName == "slash" and scope == Player.HistoryPhase then
      if table.contains(player:getTableMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo").buff or {}, 2) then
        return 1
      end
    end
  end
})
guanwei:addEffect("maxcards", {
  correct_func = function(self, player)
    if player and table.contains(player:getTableMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo").buff or {}, 3) then
      return 1
    end
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d15j__guanwei"] = "观微",
  [":rfenghou_3d15j__guanwei"] = "锁定技，游戏开始时，你摸六张牌，然后将六张牌每两张为一有序组置于武将牌上，依次称为“毫”“末”。若你有花色/类别/点数奇偶性相同的“毫末”，你的额定摸牌数/使用【杀】的限制次数/手牌上限+1。",

  ["#rfenghou_3d15j__guanwei-invoke"] = "观微：请<font color='red'>有序地</font>选择两张牌置于武将牌上，当前为第%arg组“毫末”",

  ["#rfenghou_3d15j__panjun_haomo"] = "毫末",

  ["@rfenghou_3d15j__panjun_haomo-inhand"] = "毫末",

  ["@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo"] = "毫末", -- 合并失败
  ["#rfenghou_3d15j__panjun_haomo_haomo"] = "毫-%arg-末",
  ["#rfenghou_3d15j__panjun_haomo_buff_none"] = "激昂",
  ["#rfenghou_3d15j__panjun_haomo_buff_draw"] = "英姿",
  ["#rfenghou_3d15j__panjun_haomo_buff_time"] = "诈降",
  ["#rfenghou_3d15j__panjun_haomo_buff_maxs"] = "克己",
}

return guanwei
